﻿using UnityEngine;
using System.Collections;

namespace UI
{
    public class UIUnionFightStater : MonoBehaviour
    {
        public UILabel currState;
        public UILabel timerLabel;
        public UILabel timerLabel2;
        public UILabel reviveTimerLabel;
        private float currTime;
        public float totalTime;

        bool isCountDown;
        public float Timer
        {
            set
            {
                this.currTime = value;
                if (currTime < 0f) currTime = 0f;

                var hour = (int)(currTime / (60f * 60f));
                var minute = (int)(currTime / 60f) % 60;
                var second = (int)currTime % 60;
                if (UIUnionFightRunMap.GetNetData().netBattle.state == UnionFightState.Ready && hour == 0 && minute == 0 && second == 5 && !isCountDown)
                {
                    isCountDown = true;
                    UIUnionFightRunMap.GetNetData().netBattle.CreateCountDown();
                }
                var _Str = GetStr(hour) + ":" + GetStr(minute) + ":" + GetStr(second);
                this.timerLabel.text = _Str;
                timerLabel2.text = _Str;
            }
            get
            {
                return currTime;
            }
        }

        string GetStr(int num)
        {
            if (num < 10) return "0" + num;
            else
                return num + "";
        }


        void Update()
        {
            if (Timer > 0f)
            {
                Timer -= Time.deltaTime;
               
            }

            if(ReviveTimer > 0)
            {
                ReviveTimer -= Time.deltaTime;
            }
        }

        public void Init()
        {

        }

        public void Refresh()
        {
            var net = UIUnionFightRunMap.GetNetData();
            if (net != null)
            {
                Timer = net.netBattle.curTime * 0.001f;
                switch(net.netBattle.state)
                {
                    case UnionFightState.None:
                        currState.text = "";
                        break;
                    case UnionFightState.Ready:
                        currState.text = TableTextBlo.GetText("union_fight_state_ready");
                        break;
                    case UnionFightState.Fight:
                        currState.text = TableTextBlo.GetText("union_fight_state_fighting");
                        break;
                    case UnionFightState.End:
                        currState.text = TableTextBlo.GetText("union_fight_state_end");
                        break;
                }
            }
        }

        private float curReviveTime;

        public float ReviveTimer
        {
            set
            {
                this.curReviveTime = value;
                if (curReviveTime < 0f)
                {
                    curReviveTime = 0f;
                    reviveTimerLabel.gameObject.SetActive(false);
                    return;
                }
                var hour = (int)(curReviveTime / (60f * 60f));
                var minute = (int)(curReviveTime / 60f) % 60;
                var second = (int)curReviveTime % 60;

                var _Str = GetStr(hour) + ":" + GetStr(minute) + ":" + GetStr(second);
                reviveTimerLabel.text = _Str;
            }
            get
            {
                return curReviveTime;
            }
        }



        public void SetReviveTime(float time)
        {
            ReviveTimer = time;
            reviveTimerLabel.gameObject.SetActive(true);
        }
    }
}